/**
 * copyright geolabs 2007
 * author: Gérald Fenoy
 *
 */

/**
 * MultipleSelectAutoOptions Class
 */ 
MultipleSelectAutoOptions=SelectAutoOptions.extend();

MultipleSelectAutoOptions.define({
  _init: function(){
      this.callback=arguments[2];
      this.dependents=arguments[3];
      this.setName(arguments[0]);
      this.setInitialValues(arguments[1]);
      this.request=null;
    },

  newData: function(){
      var xmldoc = this.request.responseXML;
      var rnode = xmldoc.getElementsByTagName('novalue');

      if(rnode.length>0){
	for(var i=0;i<this.dependents.length;i++){
	  this.dependents[i].empty();
	  this.dependents[i].root.parentNode.style.display='none';
	}
	this.request=null;
	this.callback();
      }
      else{
	var haschange=null;
	if(xmldoc.getElementsByTagName(this.name).length>0 && xmldoc.getElementsByTagName(this.name)[0].getElementsByTagName('tuple').length>1){
	  var tmp=document.getElementById(this.name);
	  this.empty();
	  SelectOptions.fill(tmp,xmldoc.getElementsByTagName(this.name)[0].getElementsByTagName('tuple'));
	  tmp.parentNode.style.display='block';
	  tmp.onchange=null;
	  tmp.onchange=this.activate.bind(this);
	  haschange=true;
	}
	else{
	  this.empty();
	  document.getElementById(this.name).style.display='none';
	}
	for(var i=0;i<this.dependents.length;i++){
	    this.dependents[i].empty();
	    if(xmldoc.getElementsByTagName(this.dependents[i].name).length>0 && xmldoc.getElementsByTagName(this.dependents[i].name)[0].getElementsByTagName('tuple').length>0){
	      SelectOptions.fill(this.dependents[i].root,xmldoc.getElementsByTagName(this.dependents[i].name)[0].getElementsByTagName('tuple'));
	      if(!haschange)
		this.dependents[i].root.parentNode.style.display='block';
	  }
	}
      }
      this.request=null;
    },
      
  activate: function(){
      var tmp=document.getElementById(this.name);
      var val=tmp.options[tmp.selectedIndex].value;
      //alert("VAL = "+val);
      for(var i=1;i<tmp.options.length;i++)
	if(val!=tmp.options[i].value){
	  var toto=document.getElementById(tmp.options[i].value);
	  toto.selectedIndex=0;
	  toto.parentNode.style.display="none";
	}
      var toDisplay=document.getElementById(val);
      if(toDisplay)
	toDisplay.parentNode.style.display='block';
    }
  });
